// 菜单控制
function showMenu() {
    hideAllScreens();
    document.getElementById('gameMenu').classList.remove('hidden');
    
    // 停止游戏
    if (game) {
        game.gameRunning = false;
    }
}

function showGame() {
    hideAllScreens();
    document.getElementById('gameScreen').classList.remove('hidden');
    
    // 开始游戏
    game.startGame();
}

function showLevelEditor() {
    hideAllScreens();
    document.getElementById('levelEditor').classList.remove('hidden');
    
    // 初始化编辑器
    initEditor();
}

function showScores() {
    hideAllScreens();
    document.getElementById('scoresScreen').classList.remove('hidden');
    
    // 显示排行榜
    displayScores();
}

function hideAllScreens() {
    document.querySelectorAll('.screen').forEach(screen => {
        screen.classList.add('hidden');
    });
}

function displayScores() {
    const scoresList = document.getElementById('scoresList');
    const scores = JSON.parse(localStorage.getItem('tankScores') || '[]');
    
    if (scores.length === 0) {
        scoresList.innerHTML = '<p>暂无记录</p>';
        return;
    }
    
    scoresList.innerHTML = scores.map((score, index) => `
        <div class="score-item">
            <span>${index + 1}. ${score.score}分</span>
            <span>${score.date}</span>
        </div>
    `).join('');
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', () => {
    showMenu();
    
    // 设置默认工具激活
    document.querySelector('[data-tool="brick"]').classList.add('active');
});

// 键盘事件
document.addEventListener('keydown', (e) => {
    if (e.key === 'Escape') {
        showMenu();
    }
});
